# -*- coding: utf-8 -*-
# @Time    : 2018-05-16 11:15
# @Author  : Yang Feng
# @Email   : yangfeng@bbdservice.com
# @File    : Test.py
# @Software: PyCharm

import json
import urllib2
import urllib
import codecs
import os


ratingRequest = json.loads(u"{\"ratingRequest\":{\"applyNumber\":\"OI2018050500001\",\"companyName\":\"测试IG公司\",\"creditCode\":\"915001187428619429\",\"regNumber\":\"\",\"legalPerson\":\"敖霞\",\"legalPersonIdCard\":\"510102196706108511\",\"sceneCode\":\"YSD\",\"amount\":730000,\"timeLimit\":20,\"cellPhoneNum\":\"18211112222\",\"repaymentWay\":\"先息后本\",\"accessToken\":\"KINBxxxxxxxxxx\",\"yancaozhengStartDate\":\"2015-03-05\",\"yancaozhengEndDate\":\"2018-09-09\",\"yanshangAuthId\":\"xxxx\",\"yanshangAccount\":null,\"yanshangPassword\":null,\"personBankBusiTypeArr\":\"\",\"compBankBusiTypeArr\":\"\",\"notAllowIndustry\":\"[{\\\"industryNo\\\":\\\"F\\\",\\\"industryName\\\":\\\"xxxx\\\"},{\\\"industryNo\\\":\\\"H\\\",\\\"industryName\\\":\\\"xxxx\\\"}]\",\"icr\":{},\"ecr\":{}}}")
# 黑名单 身份证号[11111, 22222] 统一社会信用编码[3333, 4444]
BlackList = json.loads(u"{\"BlackList\":{\"code\":\"200\",\"msg\":\"成功\",\"data\":[{\"id\":1,\"blackCustomerName\":null,\"blackCertype\":\"Ent01\",\"blackCertyno\":\"11111\"},{\"id\":1,\"blackCustomerName\":null,\"blackCertype\":\"Ent01\",\"blackCertyno\":\"22222\"},{\"id\":1,\"blackCustomerName\":null,\"blackCertype\":\"Ind01\",\"blackCertyno\":\"3333\"},{\"id\":1,\"blackCustomerName\":null,\"blackCertype\":\"Ind01\",\"blackCertyno\":\"4444\"}]}}")
url = "http://192.168.1.2:8888"


def file_name(file_dir):
    return_list = []
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            # if os.path.splitext(file)[1] == '.json':
            fileName = os.path.splitext(file)[0]
            with open(os.path.join(root, file)) as f:
                data_list = f.readlines()
                for data in data_list:
                    bs = data.split("@@")[0].decode("utf-8")
                    json_data = data.split("@@")[1]
                    json_data = json.loads(json_data)
                    if "ratingRequest" not in json_data.keys():
                        json_data.update(ratingRequest)
                    json_data.update(BlackList)
                    # print json_data.keys()
                    json_data = json.dumps(json_data)
                    # filePath = os.path.join("/home/bbd/TT", root, u"%s%s" % (fileName, ".py"))
                    filePath = "%s%s%s" % (u"/home/bbd/TT/", root, u"/%s%s" % (fileName, ".py"))
                    data = {'file': filePath, 'variables': json_data}
                    req = urllib2.Request(url)
                    data = urllib.urlencode(data)
                    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
                    response = opener.open(req, data)
                    response_data = response.read()
                    result = json.loads(response_data, encoding='utf-8')
                    return_list.append(u"%s:%s@@%s" % (fileName, bs, json.dumps(result, ensure_ascii=False, encoding='utf-8')))
    f = codecs.open('out.txt', 'a+', 'utf-8')
    for return_data in return_list:
        f.write(return_data)
        f.write("\n")



file_name("Rule")

def listdir(path, list_name):  #传入存储的listfor file in os.listdir(path):
    file_path = os.path.join(path, file)
    if os.path.isdir(file_path):
        listdir(file_path, list_name)
    else:
        list_name.append(file_path)



# url = "http://192.168.1.2:8888"
#
# return_list = []
# with open("IC/test/OrderAmount_FallingRate_Check/OrderAmount_FallingRate_Check.json") as f:
#     data_list = f.readlines()
#     for data in data_list:
#         bs = data.split("@@")[0].decode("utf-8")
#         json_data = data.split("@@")[1]
#         data = {'file': "/home/bbd/Rule53_OrderAmount_FallingRate_Check_2.py", 'variables': json_data}
#         req = urllib2.Request(url)
#         data = urllib.urlencode(data)
#         opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
#         response = opener.open(req, data)
#         response_data = response.read()
#         result = json.loads(response_data, encoding='utf-8')
#         return_list.append(u"%s@@%s" % (bs, json.dumps(result, ensure_ascii=False, encoding='utf-8')))

